package cn.kais.drawer.bean;

import cn.kais.drawer.menu.MenuBean;
import cn.kais.drawer.unit.UnitPicture;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.awt.*;
import java.awt.image.BufferedImage;

public class BeanPicture extends BeanBasic {

    public String src = "https://g.csdnimg.cn/common/csdn-footer/images/badge.png"; // 图片地址
    public String dft = ""; // 缺省图

    public int top;
    public int left;
    public int width;
    public int height;

    public Graphics2D sd; // 绘制

    public BeanPicture(Graphics2D sd, JSONObject com) {
        menu = MenuBean.PICTURE;
        this.sd = sd;
        this.top = com.getInteger("top");
        this.left = com.getInteger("left");
        this.width = com.getInteger("width");
        this.height = com.getInteger("height");

        this.src = com.getString("text");

        JSONArray list = com.getJSONArray("imgList");
        if (list != null && !list.isEmpty()) {
            JSONObject item = list.getJSONObject(0);
            this.dft = item.getString("imgSrc");
        }
//        com.getJSONObject("clipShap"); // 圆角
        this.create();
    }

    private void create() {
        // 区域框
        if (this.isShowBorder) {
            new BeanBorder(this.sd, this.top, this.left, this.width, this.height);
        }
        BufferedImage ima = UnitPicture.iPictureBuffer(this.src, this.dft);
        if (ima != null) {
            sd.drawImage(ima, this.left, this.top, this.width, this.height, null);
        }

    }

}
